<!DOCTYPE html>
<html lang="en">
<head>
    <link href="../vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="../vendors/jquery/dist/jquery.min.js"></script>
    <script src="../vendors/bootstrap/extend/Message_Bootstrap.min.js"></script>
    <script src="../vendors/bootstrap/extend/Common.js"></script>
</head>
<body>
<table>
    <tr>
        <td>名称</td><td><input type="text"  id="name"/></td>
    </tr>
    <tr>
        <td>容器端口</td><td><input type="text"  id="containerport"/></td>
    </tr>
    <tr>
        <td>主机映射端口</td><td><input type="text"  id="nodeport"/></td>
    </tr>
    <tr>
        <td>镜像版本</td><td><select id="imagetag">
        <option>8.2</option>
        <option>7.5</option>
    </select></td>
    </tr>
    <tr>
        <td>最大cpu</td><td><input type="text"  id="maxcpu"/></td>
    </tr>
    <tr>
        <td>最大内存</td><td><input type="text"  id="maxmenory"/></td>
    </tr>
    <tr><td rowspan="2"><input type="button"  value="提交" onclick="checkSelectItems()" class="btn btn-default"/></td></tr>
</table>
<script>
    //获取url中的参数
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]); return null; //返回参数值
    }
    function generateMixed(n) {
        var chars = ['0','1','2','3','4','5','6','7','8','9','a','c','d','e','b','f'];
        var res = "";
        for(var i = 0; i < n ; i ++) {
            var id = Math.ceil(Math.random()*15);
            res += chars[id];
        }
        return res;
    }
    function checkSelectItems() {
        var storage = window.localStorage;
        var data = storage.kuberpods;
        data = JSON.parse(data);
        var obj = new Object();
        obj.method = "get";
        obj.namespace = getUrlParam("namespace");
        obj.podIP = "172.17.12." + Math.ceil(Math.random() * 190);
        obj.hostIP = generatehost(1);
        obj.containerPort = $("#containerport").val();
        obj.nodePort=$("#nodeport").val();
        obj.status = "Running";
        obj.name = $("#name").val();
        obj.hostname = data.hostIP;
        obj.image = getUrlParam("image")+$("#imagetag").val();
        obj.maxCPU=$("#maxcpu").val();
        obj.maxMenory=$("#maxmenory").val();
        obj.uid = generateMixed(8) + "-" + generateMixed(4) + "-" + generateMixed(4) + "-" + generateMixed(8);
        if(getUrlParam("uid")==null||getUrlParam("uid")=="null") {

            data.push(obj);
            storage.kuberpods = JSON.stringify(data);
            window.parent.window.timedMsg();
            $.modalzClose();
        }
        else
        {
            obj.uid =  getUrlParam("uid");
            for(var i=0;i<data.length;i++)
            {
                if(data[i].uid==getUrlParam("uid"))
                {
                    data[i].name= $("#name").val();
                    data[i].containerPort= $("#containerport").val();
                    data[i].image = getUrlParam("image")+$("#imagetag").val();
                    data[i].nodePort=$("#nodeport").val();
                    data[i].maxCPU=$("#maxcpu").val();
                    data[i].maxMenory=$("#maxmenory").val();
                    storage.kuberpods = JSON.stringify(data);
                    window.parent.window.timedMsg();
                    $.modalzClose();
                }
            }

        }
    }
    function generatehost(n) {
        var chars = ['10.142.20.208','10.142.20.207'];
        var res = "";
        for(var i = 0; i < n ; i ++) {
            var id = Math.ceil(Math.random());
            res += chars[id];
        }
        return res;
    }
    $(function () {
        debugger
        if(getUrlParam("uid")!=null&&getUrlParam("uid")!="null") {
            var storage = window.localStorage;
            var data = JSON.parse(storage.kuberpods);

            for(var i=0;i<data.length;i++) {

                if(data[i].uid==getUrlParam("uid")) {
                    $("#name").val(data[i].name);
                    $("#containerport").val(data[i].containerPort);
                    $("#imagetag").val(data[i].image);
                    $("#nodeport").val(data[i].nodePort);
                    $("#maxcpu").val(data[i].maxCPU);
                    $("#maxmenory").val(data[i].maxMenory);

                }
            }
        }

    })
</script>
</body>
</html>